
#include "queue_frame.h"

#include "../lib_mpp/lib_mpp.h"

#define CODEC_ALIGN(x, a)   (((x)+(a)-1)&~((a)-1))

static  frame_data_t  frame_queue;


int queue_frame_init(void)
{
	int i = 0;

	log_n("frame queue init.\n");

	for(i=0;i<MAX_FRAME_SIZE_4K;i++)
	{
		frame_queue.packet[i].data = NULL;
		frame_queue.packet[i].size = 0;
		frame_queue.packet[i].w = 0;
		frame_queue.packet[i].h = 0;
	}
	
	frame_queue.windex    = 0;
	frame_queue.rindex    = 0;

	frame_queue.total     = 0; 
	frame_queue.remain    = 0;

	return 0;
}

int queue_frame_write(void * frame_data,int pts)
{
	int size   = 0;
	int w      = 0;
	int h      = 0;

	char * src_data = NULL;
	char * dst_data = NULL;

	if(!frame_data){
		log_e("data error\n");
		return -1;
	}
	
	log_n("windex=%d\n",frame_queue.windex);
	log_n("data0  =%x\n",frame_queue.packet[0].data);
	log_n("data1  =%x\n",frame_queue.packet[1].data);

    w    = mpp_frame_get_width(frame_data);
    h    = mpp_frame_get_height(frame_data);
	size = mpp_buffer_get_size(mpp_frame_get_buffer(frame_data));

	log_n("w=%d\n",w);
	log_n("h=%d\n",h);
	log_n("size=%d\n",size);
	

	mpp_buffer_get(NULL, &frame_queue.packet[frame_queue.windex].data, size); //申请内存

	log_n("3 data=%x\n",frame_queue.packet[frame_queue.windex].data);

	src_data = mpp_buffer_get_ptr(mpp_frame_get_buffer((MppFrame)frame_data));
	dst_data = mpp_buffer_get_ptr(frame_queue.packet[frame_queue.windex].data);

	//memcpy(dst_data,src_data,size);

	w = CODEC_ALIGN(w, 16);
    h = CODEC_ALIGN(h, 16);

	rga_data_copy(src_data,dst_data,w,h);

	log_n("4\n");

	frame_queue.packet[frame_queue.windex].size = size;
	frame_queue.packet[frame_queue.windex].w    = w;
	frame_queue.packet[frame_queue.windex].h    = h;
	frame_queue.packet[frame_queue.windex].pts  = pts;

	frame_queue.total = frame_queue.total + size;
	
	frame_queue.windex ++;
	if(frame_queue.windex >= MAX_FRAME_SIZE_4K)
	{
		frame_queue.windex = 0;
	}

	return 0;

}


int queue_frame_read(uint8_t **packet,int* size,int* w,int* h,int* pts)
{	
	*size   = 0;
	*packet = NULL;

	if(frame_queue.windex == frame_queue.rindex)
	{
		return -1;
	}

	if(frame_queue.windex <= 0)
	{
		return -1;
	}	
	
	*packet = frame_queue.packet[frame_queue.rindex].data;
	*size   = frame_queue.packet[frame_queue.rindex].size;
	*w      = frame_queue.packet[frame_queue.rindex].w;
	*h      = frame_queue.packet[frame_queue.rindex].h;
	*pts    = frame_queue.packet[frame_queue.rindex].pts;

	if(frame_queue.total > frame_queue.packet[frame_queue.rindex].size)
	{
		frame_queue.remain = frame_queue.total - frame_queue.packet[frame_queue.rindex].size;
	}
	else
	{
		frame_queue.remain = frame_queue.packet[frame_queue.rindex].size;
	}
	
	frame_queue.rindex++;
	if(frame_queue.rindex >= MAX_FRAME_SIZE_4K)
	{
		frame_queue.rindex = 0; 
	}
	
	return 0;
}


int queue_frame_free(void)
{
	int index = 0;

	if(frame_queue.rindex <= 0)
		return -1;

	index = frame_queue.rindex - 1;

	if(frame_queue.packet[index].data)
	{
		mpp_buffer_put(frame_queue.packet[index].data);
		frame_queue.packet[index].data = NULL;
	}

	return 0;
}


int queue_frame_is_empty(void)
{	
	if(frame_queue.windex == frame_queue.rindex)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

int queue_frame_get_total(int64_t* total)
{
	*total = frame_queue.total;

	return 0;
}

int queue_frame_get_remain(int64_t* remain)
{
	*remain = frame_queue.remain;

	return 0;
}


int queue_frame_deinit(void)
{
	int i = 0;

	log_n("audio pcm queue exit.\n");
	frame_queue.windex = 0;
	frame_queue.rindex = 0;
	
	for(i=0;i<MAX_FRAME_SIZE_4K;i++)
	{
		if(frame_queue.packet[i].data)
		{
			mpp_buffer_put(frame_queue.packet[i].data);
			frame_queue.packet[i].data = NULL;
		}
		
		frame_queue.packet[i].size = 0;	
	}
	

	return 0;
}




